Skip to content

fix: cap fs_read_bytes allocation and __hl_sleep duration - #33

Merged
danbugs merged 1 commit into
mainfrom
fix/resource-limits
May 15, 2026
Merged

fix: cap fs_read_bytes allocation and __hl_sleep duration#33
danbugs merged 1 commit into
mainfrom
fix/resource-limits

Conversation

@danbugs

@danbugs danbugs commented May 15, 2026

Copy link
Copy Markdown
Contributor

Summary

Two guest-controlled resource exhaustion vectors:

  • fs_read_bytes allocated vec![0u8; len] with no upper bound — a guest
    sending len: 999999999999 would attempt a ~1 TB host allocation. Now capped
    at 16 MiB (MAX_FS_READ), matching the pattern net_recv already uses.
  • __hl_sleep called thread::sleep(Duration::from_nanos(ns)) with no cap — a
    guest could block the host thread for up to 584 years. Now capped at 60 seconds
    (MAX_SLEEP_NS).

Both caps applied in FsSandbox::register and FsRouter::register (fs) and
register_internal_tools (sleep).

Test plan

  • test_fs_read_bytes_capped — request 1 TiB read on 5-byte file, completes without OOM
  • test_sleep_capped — cap constant verified, handler dispatches successfully
  • All existing tests pass

Signed-off-by: danbugs <danilochiarlone@gmail.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds upper bounds to two guest-controlled resource operations to prevent host resource exhaustion: a 16 MiB cap on fs_read_bytes allocations and a 60-second cap on __hl_sleep durations.

Changes:

  • Introduce MAX_FS_READ (16 MiB) and MAX_SLEEP_NS (60 s) constants and apply them via .min() in the three relevant handlers (FsSandbox::register, FsRouter::register, register_internal_tools).
  • Add unit tests test_fs_read_bytes_capped and test_sleep_capped.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Linux Benchmarks

Details
Benchmark suite Current: e6e011c Previous: 79b32ae Ratio
hello_world (median) 20 ms 20 ms 1
pandas (median) 110 ms 100 ms 1.10
density (per VM) 7 MB 7 MB 1
snapshot (disk) 385 MiB 385 MiB 1

This comment was automatically generated by workflow using github-action-benchmark.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Windows Benchmarks

Details
Benchmark suite Current: e6e011c Previous: 79b32ae Ratio
hello_world (median) 213 ms 234 ms 0.91
pandas (median) 626 ms 708 ms 0.88
density (per VM) 6 MB 6 MB 1
snapshot (disk) 393 MiB 393 MiB 1

This comment was automatically generated by workflow using github-action-benchmark.

@danbugs
danbugs merged commit 5cbe18f into main May 15, 2026
81 checks passed
@danbugs
danbugs deleted the fix/resource-limits branch May 15, 2026 06:05
@danbugs danbugs mentioned this pull request May 15, 2026
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants